// pages/delivery/delivery.js
Page({

    /**
     * 页面的初始数据
     */
    data: {


        Title:[ 
            {
                id:0,
                name:"起点"
            },
            {
                id:1,
                name:"终点"
            },
            {
                id:2,
                name:"期望到达时间"
            },
            {
                id:3,
                name:"赏金"
            }

        ],

        carOrder:[]

    },

    //对数组按照赏金金额排列
    sortArray: function(List){
        for(var i = 1 ;i<List.length;i++){
          for(var y =0 ;y<List.length-1;y++){
            if(List[y].reward>List[y+1].reward){
              var obj=List[y]
              List[y]=List[y+1]
              List[y+1]=obj
            }
          }
        }
        return List
      },

    //接单函数  
    takeorder:function(e){
        var that=this
        wx.showModal({
          title:"接单",
          content:"是否接受订单",
          success(res){
              if(res.confirm){ 
                wx.request({
                //   url: 'http://localhost:10002/car/receiveOrder',
                  url: 'http://119.23.252.15:10002/car/receiveOrder',
                  method:"POST",
                  data:{
                    "token":wx.getStorageSync('token'),
                    "orderId":e.currentTarget.id
                  },
                  success(res){
                    if(res.data.status){
                      that.onLoad()
                      wx.showToast({
                        title: '成功接受订单',
                      })
                      }
                      else{
                          wx.showToast({
                            title: '接受订单失败 似乎出现了什么BUG',
                          })
                      }
                  }
                })

              }
              else if (res.cancel)
              {
                  console.log('不接单')
              }
          },
          
        })
    },

    onShow(){
        var that =this
        var carOrder=that.sortArray(that.data.carOrder)
        that.setData({
            carOrder:carOrder
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLoad: function () {
        var that = this
        wx.request({
            //   url: 'http://localhost:10002/car/unreceiveOrderList',
              url:'http://119.23.252.15:10002/car/unreceiveOrderList',
              method:"POST",
              data:{
                "token": wx.getStorageSync('token'),
                 },
              success(res){
                console.log("获取待接约车订单")
                console.log(res.data)
               that.setData({
                   carOrder:res.data.data.unreceiveOrderList
                      })
                  }
            })
    },


})